package b3.widget.store
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	import b3.widget.warehouse.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class PurchaseDialog extends BasicWidget
	{
		public static const TYPE_PURCHASE:int = 0;
		public static const TYPE_SELL:int = 1;
    	private var type:int;
    	private var objectIndex:int;
		private var numSel:NumberSelecter;

    	//type = 0 买, type = 1 卖
		public function PurchaseDialog(objectIndex:int, type:int)
		{
			super();
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			this.objectIndex = objectIndex;
			this.type = type;
			var objectConfig:ObjConfig = Config.objConfigs[objectIndex];
			//背景
			var PurchaseDialogBgr:Class = ResourceHelper.getInstance().getClassByName("MessageBoxBgr3");
			var dialogSprite:Sprite = new PurchaseDialogBgr();
			addChild(dialogSprite);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 18, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x0088FF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			if (type == 0)
				titleText.text = "购买";
			else
				titleText.text = "售出";
			titleText.x = (dialogSprite.width - titleText.width) / 2;
			titleText.y = 16;
			dialogSprite.addChild(titleText);
			//物品图标
			var ClassImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage[0]);
			var imageSprite:Sprite = new ClassImage();
			imageSprite.width = 85;
			imageSprite.scaleY = imageSprite.scaleX;
			imageSprite.x = 70;
			imageSprite.y = 70;
			dialogSprite.addChild(imageSprite);
			
			//加载名字
			var name:TextField = new TextField();
			name.selectable = false;
			name.autoSize = TextFieldAutoSize.CENTER;
			name.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000, true);
//			name.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			name.x = 235;
			name.y = 52;
			name.text = objectConfig.name;
			dialogSprite.addChild(name);
			
			//加载价格图标
			var priceSprite:Sprite;
			var priceIconStr:String;
			var price:int;
			//购买
			if (type == TYPE_PURCHASE)
			{
				if (objectConfig.priceGoldIngot > 0)
				{
					priceIconStr = "GoldIngotIcon";
					price = objectConfig.priceGoldIngot; 
				}
				else if (objectConfig.priceGoldCoin > 0)
				{
					priceIconStr = "GoldCoinIcon";
					price = objectConfig.priceGoldCoin; 
				}
				else if (objectConfig.priceLight > 0)
				{
					priceIconStr = "LightIcon";
					price = objectConfig.priceLight; 
				}
				else
				{
					priceIconStr = "DarkIcon";
					price = objectConfig.priceDark; 
				}
				if (objectConfig.priceObjectIndex > 0)
				{
					var priceObjConfig:ObjConfig = Config.objConfigs[objectConfig.priceObjectIndex];
					priceIconStr = priceObjConfig.iconImage[0];
				}  
				price = objectConfig.discount > 0 ? price * objectConfig.discount / 100 : price;
				priceSprite = Store.getPriceSprite("价格:", price, priceIconStr, 0xFFFFFF);
			}
			//卖出
			else
			{
			}
			//加载价格
			priceSprite.x = 240 - priceSprite.width / 2;
			priceSprite.y = 75;
			dialogSprite.addChild(priceSprite);			
			
			//加载数量选择器
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			if (type == 0)
			{
				numSel = new NumberSelecter(LeftArrowButton, RightArrowButton);
			}
			else
			{
			}
			dialogSprite.addChild(numSel);
			numSel.x = 190;
			numSel.y = 130;
			
			var gap:int = -60; 
			//确定
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton;
			if (type == 0)
			{
				buttonOK = new BasicButton(ButtonBgr1, "购买");
			}
			else if (type == 1)
			{
				buttonOK = new BasicButton(ButtonBgr1, "出售");
			}
			buttonOK.x = 80;
			buttonOK.y = 170;
			dialogSprite.addChild(buttonOK);
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKMouseClick);
			
			//取消
			var buttonCancel:BasicButton = new BasicButton(ButtonBgr1, "取消");
			buttonCancel.x = 180;
			buttonCancel.y = 170;
			dialogSprite.addChild(buttonCancel);
			buttonCancel.addEventListener(MouseEvent.CLICK, onButtonCancelMouseClick);
			this.x = -this.width / 2;
			this.y = -this.height / 2;
		}
		
		private function onButtonCancelMouseClick(event:MouseEvent):void
        {
        	close();
        }
        
		private function onButtonOKMouseClick(event:MouseEvent):void
        {
			var me:Player = PlayerManager.getInstance().me;
    		//购买
        	if (type == 0)
        	{
        		me.packageModel.purchase(objectIndex, numSel.value);
        	}
    		//卖出
        	else if (type == 1)
        	{
//        		ServerHelper.getInstance().sellObject(tankId, objectIndex, basicLevel, strengthenLevel, numSel.value, price);
//				ServerHelper.getInstance().addEventListener(ServerEvent.SELL, onSellObjectComplete);
        	}
        	close();
        }
	}
}